package com.starpl.mobile.android.monthunsae.november.handler;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.starpl.mobile.android.monthunsae.november.R;
import com.starpl.mobile.android.monthunsae.november.domain.MonthUnsae;
import com.starpl.mobile.android.monthunsae.november.domain.MonthUnsaeDay;
import com.starpl.mobile.android.platform.common.DateUtil;
import com.starpl.mobile.android.platform.common.base.BaseApplication;
import com.starpl.mobile.android.platform.component.BaseListHandler;

/**
 * 일별 운세 상세보기 정보를 처리하는 리스트 핸들러
 * 
 * @author 박우람
 * @since 2011.3.17
 */
@SuppressWarnings("unused")
public class DailyUnsaeListHandler extends BaseListHandler
{
	private static final String TAG = "DailyUnsaeListHandler";
	private ArrayList<MonthUnsaeDay> mList;
	private String mToday;
	private int mSelection;
	
	public DailyUnsaeListHandler(Activity activity)
	{
		super(activity);
		
		DateUtil dateUtil = new DateUtil();
		this.mList = new ArrayList<MonthUnsaeDay>();
		this.mToday = dateUtil.toStringByFormat("dd");
		this.mSelection = 0;
	}

	@Override
	public ArrayList<?> getArrayList()
	{
		return mList;
	}

	@Override
	public View getRowView(int position, View convertView)
	{
		if(convertView == null)
		{
			convertView = inflate(R.layout.unsae_daily_default_row);
		}
		
		TextView tvDay = (TextView) convertView.findViewById(R.id.unsae_daily_default_row_tv_day);
		ImageView ivIcon = (ImageView) convertView.findViewById(R.id.unsae_daily_default_row_iv_icon);
		TextView tvContent = (TextView) convertView.findViewById(R.id.unsae_daily_default_row_tv_content);
		
		// 데이터 가져옴
		MonthUnsaeDay unsaeDay = mList.get(position);
		
		// 텍스트 설정
		tvDay.setText(unsaeDay.getDayOfMonth());
		tvContent.setText(unsaeDay.getDayContent());
		
		// 아이콘 설정
		String iconName = unsaeDay.getIconName();
		if(iconName.startsWith("용신"))
		{
			ivIcon.setBackgroundResource(R.drawable.icon_fortune_today_01);
		}
		else if(iconName.startsWith("희신"))
		{
			ivIcon.setBackgroundResource(R.drawable.icon_fortune_today_02);
		}
		else if(iconName.startsWith("희한신"))
		{
			ivIcon.setBackgroundResource(R.drawable.icon_fortune_today_03);
		}
		else if(iconName.startsWith("한신"))
		{
			ivIcon.setBackgroundResource(R.drawable.icon_fortune_today_04);
		}
		else if(iconName.startsWith("한구신"))
		{
			ivIcon.setBackgroundResource(R.drawable.icon_fortune_today_05);
		}
		else if(iconName.startsWith("구신"))
		{
			ivIcon.setBackgroundResource(R.drawable.icon_fortune_today_06);
		}
		else if(iconName.startsWith("기신"))
		{
			ivIcon.setBackgroundResource(R.drawable.icon_fortune_today_07);
		}
		else if(iconName.startsWith("양인"))
		{
			ivIcon.setBackgroundResource(R.drawable.icon_fortune_today_08);
		}
		else if(iconName.startsWith("용신양인"))
		{
			ivIcon.setBackgroundResource(R.drawable.icon_fortune_today_09);
		}
		else if(iconName.startsWith("용신희신"))
		{
			ivIcon.setBackgroundResource(R.drawable.icon_fortune_today_10);
		}
		
		// 상황에 따라 배경 이미지를 변경한다.
		if(unsaeDay.getDayOfMonth().startsWith(mToday))
		{
			int monthNumber = Integer.parseInt(BaseApplication.getConfigPersistent("monthNumber")) - 1;
			Calendar monthCal = Calendar.getInstance();
			monthCal.set(Calendar.MONTH, monthNumber);
			
			Calendar calendar = Calendar.getInstance();
			
			if(monthCal.get(Calendar.MONTH)==calendar.get(Calendar.MONTH))
			{
				// 오늘일 경우
				convertView.setBackgroundResource(R.drawable.bg_today_fortune);
			}
			else
			{
				// 오늘이 아닐 경우
				convertView.setBackgroundResource(R.drawable.bg_fortune);	
			}
			
		}
		else
		{
			// 오늘이 아닐 경우
			convertView.setBackgroundResource(R.drawable.bg_fortune);
		}
		
		// 9-패치를 사용하여 자동으로 패딩이 들어가는것을 제거한다.
		convertView.setPadding(0, 0, 0, 0);
		
		return convertView;
	}

	@Override
	public void onRowClick(View v)
	{
	}

	@Override
	public void onRowClear()
	{
		// USAGE : 리프레시등에 의하여 데이터를 삭제할경우 사용되는 로직
		mList.clear();
	}

	@Override
	public void loadContent()
	{
		MonthUnsae monthUnsae = (MonthUnsae) BaseApplication.getGlobalPersistent("monthUnsae");
		ArrayList<MonthUnsaeDay> unsaeDayList = monthUnsae.getMonthUnsaeDayList();
		
		// 값을 배열로 복제한다.
		int position = 0;
		for(MonthUnsaeDay unsaeDay : unsaeDayList)
		{
			mList.add(unsaeDay);
			
			// 오늘인 날짜를 바로 선택하기 위해 추가
			if(unsaeDay.getDayOfMonth().startsWith(mToday))
			{
				mSelection = position;
			}
			
			position++;
		}
		
		notifyDataSetChanged();
	}

	@Override
	public void onInit()
	{
		// USAGE : 어댑터를 붙인다. 실제로 데이터와 리스트뷰가 연결된다.
		setAdapter();
		
		// USAGE : 컨텐츠 수동 호출
		loadContent();
		
		int monthNumber = Integer.parseInt(BaseApplication.getConfigPersistent("monthNumber")) - 1;
		Calendar monthCal = Calendar.getInstance();
		monthCal.set(Calendar.MONTH, monthNumber);
		
		Calendar calendar = Calendar.getInstance();
		
		if(monthCal.get(Calendar.MONTH) == calendar.get(Calendar.MONTH))
		{
			// 오늘날짜를 선택
			mListView.setSelection(mSelection);
		}
		
	}
}